<?php

/**
 * This is the model class for table "blog_entry".
 *
 * The followings are the available columns in table 'blog_entry':
 * @property integer $ble_code
 * @property string $ble_title
 * @property string $ble_name
 * @property string $ble_content
 * @property string $ble_date
 * @property integer $pag_code
 *
 * The followings are the available model relations:
 * @property Page $pagCode
 * @property Comment[] $comments
 */
class BlogEntry extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return BlogEntry the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'blog_entry';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ble_title', 'required'),
			array('pag_code', 'numerical', 'integerOnly'=>true),
			array('ble_title, ble_name', 'length', 'max'=>45),
			array('ble_content', 'length', 'max'=>550),
			array('ble_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ble_code, ble_title, ble_name, ble_content, ble_date, pag_code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pagCode' => array(self::BELONGS_TO, 'Page', 'pag_code'),
			'comments' => array(self::HAS_MANY, 'Comment', 'ble_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ble_code' => 'Ble Code',
			'ble_title' => 'Ble Title',
			'ble_name' => 'Ble Name',
			'ble_content' => 'Ble Content',
			'ble_date' => 'Ble Date',
			'pag_code' => 'Pag Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ble_code',$this->ble_code);
		$criteria->compare('ble_title',$this->ble_title,true);
		$criteria->compare('ble_name',$this->ble_name,true);
		$criteria->compare('ble_content',$this->ble_content,true);
		$criteria->compare('ble_date',$this->ble_date,true);
		$criteria->compare('pag_code',$this->pag_code);
    
    $page = (int)$_GET['page'];
    if($page > 0){
          $criteria->condition = 'pag_code='.$page;
      }else{
          throw new CHttpException(404, 'The requested page does not exist.');
      }
      
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function behaviors()
  {
      return array('datetimeI18NBehavior' => array('class' => 'ext.DateTimeI18NBehavior')); // 'ext' is in Yii 1.0.8 version. For early versions, use 'application.extensions' instead.
  }
  
}